import { Component, OnInit, Injector, ViewChild, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import {
    ConsumableServiceProxy,
    CostOfConsumableEditDto,
    CreateOrUpdateCostOfConsumableInput,
    DeviceServiceProxy,
    GetCostOfConsumableForEditOutPut,
    CostOfDeviceListDto
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { CalendarModule } from 'primeng/calendar';
import { SelectItem, LazyLoadEvent } from 'primeng/api';
import { Table } from 'primeng/table';
import { Paginator } from 'primeng/primeng';

@Component({
    selector: 'createOrEditConsumablesModal',
    templateUrl: './create-or-edit-consumables-modal.component.html',
    styleUrls: ['./create-or-edit-consumables-modal.component.less']
})
export class CreateOrEditConsumablesModalComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        private _consumableServiceProxy: ConsumableServiceProxy,
        private _deviceServiceProxy: DeviceServiceProxy
    ) {
        super(injector);
    }
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    editDto = new CostOfConsumableEditDto();
    deviceListDto: CostOfDeviceListDto;
    filterText = '';
    deviceName = '';
    deviceNumber = '';
    sorting = '';
    maxResultCount?: number;
    SkipCount?: number;
    deviceId?: number;
    deviceNumberList: SelectItem[];
    deviceList = new Array<CostOfDeviceListDto>();
    ngOnInit(): void {}
    show(id?: number): void {
        this.active = true;

        this._deviceServiceProxy
            .getAllAsync(
                this.filterText,
                this.deviceName,
                this.deviceNumber,
                this.sorting,
                // this.primengTableHelper.getSorting(this.dataTable),
                1000,
                0
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
                //  console.log(this.primengTableHelper.records);

                this.deviceList = result.items;

                this.deviceNumberList = [];
                for (let i = 0; i < this.deviceList.length; i++) {
                    this.deviceNumberList.push({
                        value: this.deviceList[i].id,
                        label:
                            '设备编号:' +
                            ' ' +
                            this.deviceList[i].deviceNumber +
                            ',' +
                            ' ' +
                            '设备名称:' +
                            ' ' +
                            this.deviceList[i].deviceName
                    });
                }
            });

        this._consumableServiceProxy.getForEditAsync(id).subscribe(result => {
            this.editDto = result.costOfConsumable;
            this.deviceId = result.deviceId;
            const self = this;

            this.modal.show();
        });
    }

    save(): void {
        this.saving = true;
        let input = new CreateOrUpdateCostOfConsumableInput();
        input.costOfConsumable = this.editDto;
        input.deviceId = this.deviceId;

        this._consumableServiceProxy
            .createOrUpdateAsync(input)
            .pipe(
                finalize(() => {
                    this.saving = false;
                })
            )
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
